package com.twu.biblioteca.behavior;

import com.twu.biblioteca.Movie;
import com.twu.biblioteca.Theater;

import java.util.List;

import static com.google.common.base.Strings.padEnd;
import static java.lang.Math.max;

public class ListMoviesBehavior implements Behavior {
    @Override
    public String getMessage() {
        List<Movie> movies = Theater.getInstance().getMovies();

        int nameMaxLength = 10;
        int yearMaxLength = 10;
        int directorMaxLength = 15;
        int ratingMaxLength = 10;

        for (Movie m : movies) {
            nameMaxLength = max(nameMaxLength, m.getName().length() + 4);
            yearMaxLength = max(yearMaxLength, String.valueOf(m.getYear()).length() + 4);
            directorMaxLength = max(directorMaxLength, m.getDirector().length() + 4);
            ratingMaxLength = max(ratingMaxLength, m.getRating().length() + 4);
        }

        StringBuilder info = new StringBuilder();
        String lineSeparator = System.getProperty("line.separator");

        info.append("-------------------------------------------").append(lineSeparator);
        info.append(padEnd("Movie", nameMaxLength, ' '))
                .append(padEnd("Year", yearMaxLength, ' '))
                .append(padEnd("Director", directorMaxLength, ' '))
                .append(padEnd("Rating", ratingMaxLength, ' ')).append(lineSeparator).append(lineSeparator);

        for (Movie m : movies) {
            info.append(padEnd(m.getName(), nameMaxLength, ' '))
                    .append(padEnd(String.valueOf(m.getYear()), yearMaxLength, ' '))
                    .append(padEnd(m.getDirector(), directorMaxLength, ' '))
                    .append(padEnd(m.getRating(), ratingMaxLength, ' ')).append(lineSeparator);
        }
        info.append("-------------------------------------------").append(lineSeparator);
        return info.toString();
    }

    @Override
    public Behavior nextBehavior() {
        return new ListMenuOptionBehavior();
    }

    @Override
    public void execute() {
        System.out.println(getMessage());
    }
}
